<?php
/**
 * Created by PhpStorm.
 * User: xufei
 * Date: 2018/5/3
 * Time: 下午5:13
 */

class wxapi_ctl_goodsFavorite extends wxapi_response
{
    public $pagedata;

    public function __construct(&$app)
    {
        parent::__construct($app);
    }

    /**
     * 添加收藏
     */
    public function add()
    {
        $goods_id = $_GET['goodsId'];
        $object_type = 'goods';
        $member = $this->member_info;
        if( !($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        
        $res = kernel::single('b2c_member_fav')->add_fav($member['member_id'],$object_type,$goods_id);
        if ( ! $res){
            $return = [
                'result' => '商品加入收藏失败',
                'code' => 5001,
            ];
            return self::return_json($return);
        }else{
            $return = [
                'result' => '0',
                'code' => 0,
            ];
            return self::return_json($return);
        }
    }

    /**
     * 删除收藏
     */
    public function delete()
    {
        $gid = $_GET['goodsId'];
        $object_type = 'goods';
        if(!$gid){
            $return = [
                'result' => '商品ID错误',
                'code' =>  5001,
            ];
            return self::return_json($return);
        }

        $member = $this->member_info;
        if(empty($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }

        if (!kernel::single('b2c_member_fav')->del_fav($member['member_id'],$object_type,$gid,$maxPage)){
            $return = [
                'result' => '移除失败',
                'code' =>  5001,
            ];
            return self::return_json($return);
        }else{
            $aData = kernel::single('b2c_member_fav')->get_favorite($member['member_id'],$member['member_lv']);
            $aProduct = $aData['data'];
            $oImage = app::get('image')->model('image');
            $imageDefault = app::get('image')->getConf('image.set');
            foreach($aProduct as $k=>$v) {
                if(!$oImage->getList("image_id",array('image_id'=>$v['image_default_id']))){
                    $aProduct[$k]['image_default_id'] = $imageDefault['S']['default_image'];
                }
            }
            $this->pagedata['favorite'] = $aProduct;
            $this->pagedata['defaultImage'] = $imageDefault['S']['default_image'];
            $return = [
                'result' => '移除成功',
                'code' =>  0,
            ];
            return self::return_json($return);
        }
    }

    /**
     * 是否收藏
     */
    public function isFavorite()
    {
        $goods_id = intval($_GET['goodsId']);
        $member = $this->member_info;
        if( ! $member['member_id']){
            //未收藏
            $return = [
                'reason' => '',
                'code' => 0,
                'isFavorite' => 0,
            ];
            return self::return_json($return);
        }

        $obj_member = app::get('b2c')->model('member_goods');
        $aData = $obj_member->get_member_goods($member['member_id'], $goods_id);
        $return = [
            'reason' => '',
            'code' => 0,
            'isFavorite' => count($aData) ? 1 : 0,
        ];
        return self::return_json($return);
    }

    /**
     * 用户收藏的商品
     * @return mixed
     */
    public function favoriteInfo()
    {
        $member = $this->member_info;
        if( !($member['member_id'])){
            $return = [
                'result' => '请先登录',
                'code' => 5001,
            ];
            return self::return_json($return);
        }
        $page = $_POST['page'] ? $_POST['page'] : 1;
        $size = $_POST['size'] ? $_POST['size'] : 10;
        $unionId = $_POST['unionId'];
        $obj_member = app::get('b2c')->model('member_goods');
        $favs = $obj_member->get_favorite($member['member_id'], $member['member_lv'], $page, $size);
        $goodsArr = [];
        if (!empty($favs)){
            foreach($favs['data'] as $v){
                $goods_ids[] = $v['goods_id'];
                $goodsItem = [];
                $goodsItem['price']    =    round($v['price'], 2);
                $goodsItem['goodsId']  =    $v['goods_id'];
                $goodsItem['name']     =    $v['name'];
                $goodsItem['siteId']   =    '';
                $goodsItem['thumLogo'] =    base_storager::image_path($v['image_default_id']);
                $goodsItem['type']     = 2;
                $goodsArr[] = $goodsItem;
            }
            
            if($goods_ids) {
                $mdl_goods = app::get('b2c')->model('goods');
                $rs_goods = $mdl_goods->getList('goods_id,price', array('goods_id'=>$goods_ids));
                foreach($rs_goods as $v){
                    $goods_price[$v['goods_id']] = $v['price'];
                }
                
                foreach($goodsArr as &$v){
                    if(isset($goods_price[$v['goodsId']])){
                        $v['price'] = ($goods_price[$v['goodsId']]);
                    }
                }
            }
        }
        $return = [
            'reason' => '',
            'code' => 0,
            'list' => $goodsArr,
        ];
        return self::return_json($return);
    }
    
}